<template>
  <div class="p-6px">
    <el-card class="rounded-md" shadow="never">
      <!-- 搜索条件 -->
      <el-form v-show="showSearch" :inline="true" id="searchFormId" @submit.prevent>
        <el-form-item label="日期" prop="date">
          <el-date-picker v-model="searchParams.date" type="date" placeholder="请选择日期" />
          <el-button @click="selectPreviousDay" style="margin-left: 10px">前一天</el-button>
          <el-button @click="selectNextDay">后一天</el-button>
        </el-form-item>
        <el-form-item label="request_id" prop="request_id">
          <el-input placeholder="请输入request_id" v-model="searchParams.request_id" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="APP_KEY" prop="APP_KEY">
          <el-input placeholder="请输入APP_KEY" v-model="searchParams.app_key" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" plain v-debounce="handleSearch">搜索</el-button>
          <el-button type="danger" icon="refresh" plain v-throttle="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 表格头部按钮 -->
      <el-row :gutter="10">
        <el-col :span="1.5" v-auth="['system:role:add']"> </el-col>
        <KoiToolbar v-model:showSearch="showSearch" @refresh-table="handleListPage"></KoiToolbar>
      </el-row>

      <br />
      <el-table v-loading="loading" v-adaptive border :data="tableList" empty-text="暂时没有数据哟">
        <el-table-column label="request_id" prop="request_id" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="APP_KEY" prop="app_key" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="method" prop="method" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="ip" prop="ip" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="cost_time" prop="cost_time" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="transfer" prop="transfer" align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" align="left" fixed="right" v-auth="['system:role:update', 'system:role:delete']">
          <template #default="{ row }">
            <el-button type="primary" size="small" plain @click="handleDetail(row)" v-auth="['system:role:update']">
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <br />
      <!-- 分页 -->
      <el-pagination
        background
        v-model:current-page="searchParams.pageNo"
        v-model:page-size="searchParams.pageSize"
        v-show="total > 0"
        :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleListPage"
        @current-change="handleListPage"
      />

      <KoiDrawer ref="koiDrawerRef" title="详情" :size="1000" footer-hidden>
        <template #content>
          <json-editor mode="text" v-model:json="form.details" />
        </template>
      </KoiDrawer>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="memberLevelPage">
import JsonEditor from "vue3-ts-jsoneditor";

import { listPage, read } from "@/plugin/coleopenapi/api/requestlog/index.ts";
import { koiNoticeError } from "@/utils/koi.ts";
import { onMounted, ref } from "vue";
import { dayjs } from "element-plus";

// 数据表格加载页面动画
const loading = ref(false);
/** 是否显示搜索表单 */
const showSearch = ref<boolean>(true); // 默认显示搜索条件
// 数据表格数据
const tableList = ref<any>([]);
// 查询参数
const searchParams = ref({
  pageNo: 1, // 第几页
  pageSize: 10, // 每页显示多少条
  request_id: "",
  app_key: "",
  date: new Date()
});

const total = ref<number>(0);
const selectPreviousDay = () => {
  const newDate = new Date(searchParams.value.date);
  newDate.setDate(newDate.getDate() - 1);
  searchParams.value.date = newDate;
  handleListPage();
};
const selectNextDay = () => {
  const newDate = new Date(searchParams.value.date);
  newDate.setDate(newDate.getDate() + 1);
  searchParams.value.date = newDate;
  handleListPage();
};

// 重置搜索参数
const resetSearchParams = () => {
  searchParams.value = {
    pageNo: 1,
    pageSize: 10,
    request_id: "",
    app_key: "",
    date: new Date()
  };
};

/** 搜索 */
const handleSearch = () => {
  searchParams.value.pageNo = 1;
  handleListPage();
};

/** 重置 */
const resetSearch = () => {
  resetSearchParams();
  handleListPage();
};

/** @current-change：点击分页组件页码发生变化：例如：切换第2、3页 OR 上一页 AND 下一页 OR 跳转某一页 */
/** @size-change：点击分页组件下拉选中条数发生变化：例如：选择10条/页、20条/页等 */
// 分页查询，@current-change AND @size-change都会触发分页，调用后端分页接口
/** 数据表格 */
const handleListPage = async () => {
  try {
    loading.value = true;
    tableList.value = []; // 重置表格数据
    let data = Object.assign({}, searchParams.value, {
      date: dayjs(searchParams.value.date).format("YYYY-MM-DD")
    });
    const res: any = await listPage(data);
    tableList.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  } catch (error) {
    console.log(error);
    koiNoticeError("数据查询失败，请刷新重试");
  }
};

onMounted(() => {
  // 获取数据表格数据
  handleListPage();
});

/** 回显数据 */
const handleDetail = async (row: any) => {
  try {
    const res: any = await read(row.id);
    form.value = res.data;
    form.value.details = JSON.parse(form.value.details);
    koiDrawerRef.value.koiOpen();
  } catch (error) {
    koiNoticeError("数据获取失败，请刷新重试");
    console.log(error);
  }
};

/** 添加 AND 修改抽屉 */
const koiDrawerRef = ref();
const form = ref();
</script>

<style lang="scss" scoped></style>
